﻿/*Guardar en el registro las opciones */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Threading;

namespace OOMetaExtractor
{
    public partial class FormOpciones : Form
    {
        public OOPlantillaDeBorrado OOPlantilla;

        public FormOpciones()
        {
            InitializeComponent();
            LoadLanguage();
            this.OOPlantilla = new OOPlantillaDeBorrado();
            LoadConfig();
        }


        private void FormOpciones_Load(object sender, EventArgs e)
        {
            LoadConfig();
        }

        private void buttonGuardar_Click(object sender, EventArgs e)
        {
            SaveConfig();
            this.Close();
        }


        private void buttonCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void listViewOO_BeforeLabelEdit(object sender, LabelEditEventArgs e)
        {
            e.CancelEdit = !Convert.ToBoolean(OOPlantillaDeBorrado.tipos[e.Item, 2]);
        }

        public void LoadConfig()
        {
            listViewOO.Items.Clear();
            RegistryKey rk = Registry.CurrentUser;
            rk = rk.CreateSubKey(@"SOFTWARE\I64\OOMetaExtractor\Configuracion\OO");
            for (int i = 0; i < OOPlantillaDeBorrado.tipos.GetLength(0); i++)
            {
                this.OOPlantilla.valor[i] = (string)rk.GetValue(OOPlantillaDeBorrado.tipos[i, 0] + "Valor", "");
                this.OOPlantilla.seleccionado[i] = Convert.ToBoolean((string)rk.GetValue(OOPlantillaDeBorrado.tipos[i, 0], "True"));

                ListViewItem lvi = listViewOO.Items.Add(this.OOPlantilla.valor[i]);
                lvi.SubItems.Add(OOPlantillaDeBorrado.tipos[i, 1]);
                lvi.Checked = this.OOPlantilla.seleccionado[i];
                lvi.UseItemStyleForSubItems = false;
                if(Convert.ToBoolean(OOPlantillaDeBorrado.tipos[i, 2]))
                    lvi.BackColor = Color.Lavender;
            }
            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo((string)rk.GetValue("AppLanguage", "en-US"));
            Program.LoadLanguage();
            rk.Close();
        }

        public void SaveConfig()
        {
            RegistryKey rk = Registry.CurrentUser;
            rk = rk.CreateSubKey(@"SOFTWARE\I64\OOMetaExtractor\Configuracion\OO");
            for (int i = 0; i < listViewOO.Items.Count; i++)
            {
                this.OOPlantilla.valor[i] = listViewOO.Items[i].Text;
                this.OOPlantilla.seleccionado[i] = listViewOO.Items[i].Checked;

                rk.SetValue(listViewOO.Items[i].SubItems[1].Text, listViewOO.Items[i].Checked);
                if (Convert.ToBoolean(OOPlantillaDeBorrado.tipos[i, 2]))  //Es un valor editable, guardamos el valor en el registro
                    rk.SetValue(listViewOO.Items[i].SubItems[1].Text + "Valor", this.OOPlantilla.valor[i]);
            }
            rk.SetValue("AppLanguage", Thread.CurrentThread.CurrentUICulture);
            rk.Close();
        }
    }
}